package logic

import (
	"encoding/json"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/helper/resphelper/placeresp"
	"yundian/internel/app/models/bindlogs"
	"yundian/internel/app/models/downloadlogs"
	"yundian/internel/app/models/orderoperatelog"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/roles"
	"yundian/internel/app/models/userprofitlog"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dingo"
)

type LogsLogic struct {
}

func NewLogsLogic() *LogsLogic {
	return &LogsLogic{}
}

// 新增订单操作日志(下订单、订单撤销、订单完结、订单回滚、订单分红、恢复订单、订单同步、部分退款)
func (logLogic *LogsLogic) InsertOrderLogLogic(param ...interface{}) {
	m := orderoperatelog.OrderOperateLog{}
	m = param[0].(orderoperatelog.OrderOperateLog)
	//m.UserId = param[0].(int)
	//m.UserName = param[1].(string)
	//m.OperateType = param[3].(uint8)
	//m.ErrorType = param[4].(int)
	//m.ErrorMessage = param[5].(string)
	m.CreateAt = dingo.Time(time.Now().Unix())

	dao.NewOrderDao().InsertOrderOperateLog(&m).Unwrap()
}

// 新增用户分成修改日志
func (logLogic *LogsLogic) InsertUserProfitLogLogic(param ...interface{}) {
	m := param[0].(userprofitlog.UserProfitLog)
	p := param[1].(*request.AdminUserInsertReq)
	u := param[2].(*users.UserModel)
	var id int
	if u.ID == 0 { id = m.Id } else { id = u.ID }
	m.CreateAt = dingo.Time(time.Now().Unix())
	pu := dao.NewUserDao().GetUserNameById(p.ParentId).Unwrap().(*users.UserModel)
	uu := dao.NewUserDao().GetUserNameById(u.ParentID).Unwrap().(*users.UserModel)
	pr := dao.NewRoleDao().GetRoleNameById(p.RoleId).Unwrap().(*roles.RolesModel)
	ur := dao.NewRoleDao().GetRoleNameById(u.RoleID).Unwrap().(*roles.RolesModel)
	// 新增保存日志的字段
	pAfterJson := users.UserChangeField{
		ID:               id,
		ParentID:         p.ParentId,
		ParentName:       pu.Name,
		ParentUserName:   pu.UserName,
		RoleID:           p.RoleId,
		RoleName:         pr.Name,
		IsAdmin:          p.IsAdmin,
		Name:             p.Name,
		UserName:         p.UserName,
		Phone:            p.Phone,
		ProfitProportion: p.ProfitProportion,
		IsDraw:           p.IsDraw,
		IsCash:           p.IsCash,
		IsDivinds:        p.IsDivinds,
		CreateAt:         dingo.Time(time.Now().Unix()),
		UpdateAt:         dingo.Time(time.Now().Unix()),
	}
	pAfterjson,_ := json.Marshal(pAfterJson)
	m.AfterField = string(pAfterjson)

	ucfBefore := users.UserChangeField{
		ID:               u.ID,
		ParentID:         u.ParentID,
		ParentName:       uu.Name,
		ParentUserName:   uu.UserName,
		RoleID:           u.RoleID,
		RoleName:         ur.Name,
		IsAdmin:          u.IsAdmin,
		Name:             u.Name,
		UserName:         u.UserName,
		Phone:            u.Phone,
		ProfitProportion: u.ProfitProportion,
		IsDraw:           u.IsDraw,
		IsCash:           u.IsCash,
		IsDivinds:        u.IsDivinds,
		CreateAt:         u.CreateAt,
		UpdateAt:         u.UpdateAt,
	}
	pBeforeJson,_ := json.Marshal(ucfBefore)
	m.BeforeField = string(pBeforeJson)
	dao.NewUserDao().InsertUserProfitLog(&m).Unwrap()
}


// 新增门店信息操作日志
func (logLogic *LogsLogic) InsertPlaceOperateLogLogic(param ...interface{}) {
	m := param[0].(placemodel.PlaceOperateLog)
	pReq := param[1].(*request.PlaceInsertReq)
	p := param[2].(*placemodel.PlaceModel)
	m.CreateAt = dingo.Time(time.Now().Unix()) // 创建时间

	directBeforeUser := dao.NewUserDao().GetUserNameById(pReq.UserId).Unwrap().(*users.UserModel)
	directAfterUser := dao.NewUserDao().GetUserNameById(p.UserID).Unwrap().(*users.UserModel)

	branchBeforeUser := dao.NewUserDao().GetUserNameById(pReq.BranchUserId).Unwrap().(*users.UserModel)
	branchAfterUser := dao.NewUserDao().GetUserNameById(p.BranchUserId).Unwrap().(*users.UserModel)

	devices := dao.NewDeviceDao().GetDeviceCountByPlaceId(p.Id).Unwrap().(int64)

	// 新增保存日志的字段
	pAfterJson := placeresp.PlaceChangeField{
		Id:             p.Id,
		UserId:         pReq.UserId,
		UserName:       directAfterUser.Name + "/" + directAfterUser.UserName,
		BranchUserId:   pReq.BranchUserId,
		BranchUserName: branchAfterUser.Name + "/" + branchAfterUser.UserName,
		PlaceName:      pReq.PlaceName,
		ClassName:      pReq.ClassName,
		Province:       pReq.Province,
		City:           pReq.City,
		County:         pReq.County,
		Address:        pReq.Address,
		EntryMoney:     pReq.EntryMoney,
		EntryAt:        pReq.EntryAt,
		HotelId:        pReq.HotelId,
		IsAd:           pReq.IsAd,
		IsOpenFirst:    pReq.IsOpenFirst,
		ProjectLabel:   pReq.ProjectLabel,
		CreateAt:       p.CreateAt,
		UpdateAt:       p.UpdateAt,
		Devices:        devices,
	}
	pAfterjson,_ := json.Marshal(pAfterJson)
	m.AfterField = string(pAfterjson)

	ucfBefore := placeresp.PlaceChangeField{
		Id:             p.Id,
		UserId:         p.UserID,
		UserName:       directBeforeUser.Name + "/" + directBeforeUser.UserName,
		BranchUserId:   pReq.BranchUserId,
		BranchUserName: branchBeforeUser.Name + "/" + branchBeforeUser.UserName,
		PlaceName:      p.PlaceName,
		ClassName:      p.ClassName,
		Province:       p.Province,
		City:           p.City,
		County:         p.County,
		Address:        p.Address,
		EntryMoney:     p.EntryMoney,
		EntryAt:        int64(p.EntryAt),
		HotelId:        p.HotelId,
		IsAd:           p.IsAd,
		IsOpenFirst:    p.IsOpenFirst,
		ProjectLabel:   p.ProjectLabel,
		CreateAt:       p.CreateAt,
		UpdateAt:       p.UpdateAt,
		Devices:        devices,
	}
	pBeforeJson,_ := json.Marshal(ucfBefore)
	m.BeforeField = string(pBeforeJson)

	dao.NewUserDao().InsertPlaceOperateLog(&m).Unwrap()
}

func (logLogic *LogsLogic) InsertDownloadLog(params ...interface{}) {
	userId := params[0].(int)
	ip := params[1].(string)
	fileName := params[2].(string)
	fileUrl := params[3].(string)
	remark := params[4].(string)

	downloadModel := downloadlogs.DownloadLogsModel{
		UserId:   userId,
		ClientIp: ip,
		CreateAt: dingo.Time(time.Now().Unix()),
		FileName: fileName,
		FileUrl:  fileUrl,
		Remark:   remark,
	}
	dao.NewDownloadLogsDao().InsertDownloadLogs(&downloadModel).Unwrap()
}

// 新增微信绑定日志
func (logLogic *LogsLogic) InsertBindLogs(params ...interface{}) {
	m := params[0].(*bindlogs.BindLogsModel)
	dao.NewBindLogsDao().InsertBindLogs(m).Unwrap()
}